import { useCallback, useEffect } from 'react';
import { useCardSDK } from '../sdk';
import Logger from '../../../../util/Logger';
import { CardVerificationState } from '../types';
import { useWrapWithCache } from './useWrapWithCache';

export interface UserKYCStatus {
  verificationState: CardVerificationState | null;
  userId: string | null;
}

interface UseGetUserKYCStatusResult {
  kycStatus: UserKYCStatus | null;
  isLoading: boolean;
  error: Error | null;
  fetchKYCStatus: () => Promise<UserKYCStatus | null>;
}

/**
 * Hook to fetch user KYC verification status
 * Only fetches when user is authenticated
 *
 * @param isAuthenticated - Whether the user is authenticated
 * @returns {UseGetUserKYCStatusResult} KYC status data, loading state, error, and fetch function
 */
const useGetUserKYCStatus = (
  isAuthenticated: boolean,
): UseGetUserKYCStatusResult => {
  const { sdk } = useCardSDK();

  const fetchKYCStatusInternal = useCallback(async () => {
    if (!isAuthenticated || !sdk) {
      return null;
    }

    try {
      const response = await sdk.getUserDetails();

      return {
        verificationState: response.verificationState ?? null,
        userId: response.id,
      };
    } catch (err) {
      const errorMessage =
        err instanceof Error ? err : new Error('Failed to fetch KYC status');
      Logger.log('useGetUserKYCStatus: Error fetching KYC status', err);
      throw errorMessage;
    }
  }, [sdk, isAuthenticated]);

  const cacheResult = useWrapWithCache('kyc-status', fetchKYCStatusInternal, {
    cacheDuration: 60 * 1000, // 60 seconds cache
    fetchOnMount: false, // Disable auto-fetch, we'll manually control it below
  });

  const { data, isLoading, error, fetchData } = cacheResult;

  // Manually trigger fetch when all prerequisites are ready
  // This avoids the race condition where SDK isn't available on first render
  useEffect(() => {
    if (sdk && isAuthenticated && !isLoading && !error && !data) {
      fetchData();
    }
    // eslint-disable-next-line react-compiler/react-compiler
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [sdk, isAuthenticated, isLoading, error, data]);

  return {
    kycStatus: data,
    isLoading,
    error,
    fetchKYCStatus: fetchData,
  };
};

export default useGetUserKYCStatus;
